import {Article} from 'knex/types/tables';
import knex from '../db/sql'
import UserController from './user.controller'
import AdminController from './admin.controller';
import {Context, MetheodType, Next} from 'TYPE';

export default class ContentController {
    static async getAllArticles(ctx: Context, next: Next) {
        let number = Number(ctx.query.number)
        number = number || 100
        await knex('articles').select().where('deleted',false).orderBy('create_time').limit(number).then(res => {
            if (!res.length) {
                ctx.cd = 1
                ctx.msg = "无文章"
            }
            else {
                ctx.cd = 0
                ctx.msg = "查找成功"
                ctx.return = res
            }
        })
        next()
    }
    static async getArticle(ctx: Context, next: Next) {
        let {aid} = ctx.query
        if (!aid) {
            ctx.cd = 1
            ctx.msg = "无文章"
        }
        await knex('articles').select().where({aid: Number(aid), deleted: false}).first().then(res => {
            if (!res) {
                ctx.cd = 1
                ctx.msg = "无文章"
            }
            else {
                ctx.cd = 0
                ctx.msg = "查找成功"
                ctx.return = res
            }
        })
        next()
    }
    @AdminController.checkIsAdmin
    static async uploadArticle(ctx: Context, next: Next) {
        let {title, content} = ctx.request.body
        console.log(title);
        console.log(content);

        if (title && content) {
            await knex('articles').insert({title, content},'*').then(res => {
                if (res.length) {
                    ctx.cd = 0
                    ctx.msg = "上传成功"
                    ctx.return = res[0]
                }
            })
        } else {
            ctx.cd = 1
            ctx.msg = "上传失败！！"
        }
        await next()
    }


    @AdminController.checkIsAdmin
    static async modifyArticle(ctx: Context, next: Next) {
        let {aid, title, content} = ctx.request.body
        if (!(aid)) {
            ctx.cd = 1
            ctx.msg = "信息不全！！"
        }
        // let image_path = null
        // if (ctx.request.files && ctx.request.files.image) {
        //     image_path = (ctx.request.files.image as any).newFilename as string
        // }

        let article: Article

        article = await knex('articles').select('*').where('aid', aid).first()

        if (!article) {
            ctx.cd = 1
            ctx.msg = "没有文章！！"
            next()
            return
        }
        article.title = title || article.title
        article.content = content || article.content
        delete article.modify_time // 数据库自动更新修改时间

        await knex('articles').where('aid', aid)
            .update(article).then((num) => {
                if (num > 0) {
                    ctx.cd = 0
                    ctx.msg = "文章修改成功"
                    ctx.return = article
                }
                else {
                    ctx.cd = 1
                    ctx.msg = "文章修改失败"
                }
            })
        await next()
    }

    @AdminController.checkIsAdmin
    static async deleteAritle(ctx: Context, next: Next) {
        let {aid} = ctx.query
        if (!aid) {
            ctx.cd = 1
            ctx.msg = "信息不全！！"
            next()
            return
        }
        await knex('articles').where('aid', aid)
            .update({deleted: true}).then((num) => {
                if (num > 0) {
                    ctx.cd = 0
                    ctx.msg = "文章删除成功"
                }
                else {
                    ctx.cd = 1
                    ctx.msg = "文章删除失败"
                }
            })
        next()
    }
}
